#include "XGLObject.h"

float* fparse(string str, int N) {
	int i = 0;
	int count = 0;
	float* out = (float*)malloc(N*sizeof(float));
	string temp = "";
	while(1) {
		if((str[i] == ',') || (count >= N)) {
			if(temp.size() > 0) {
				out[count] = (float) strtod(temp.c_str(),NULL);
				count++;
				temp = "";
			}
			if(count >= N) {
				return out;
			}
		}
		else {
			temp+=str[i];
		}
		i++;
	}
	return NULL;
}

XGLObject::XGLObject(string file, int id) {
	XMLNode xglRoot = XMLNode::openFileHelper(file.c_str(),"WORLD");
	
	XMLNode mesh = xglRoot.getChildNode("MESH",id);
	int num_vertex = mesh.nChildNode("P");
	for(int i=0; i < num_vertex; i++) {
		float* tmp;
		string temp = mesh.getChildNode("P",i).getText();
		tmp = fparse(temp,3);	//Obtain a vector with the 3 values converted
		vertices.push_back(tmp);
	}
	printf("%d\n",num_vertex);
}

XGLObject::~XGLObject() {
	vertices.clear();
}
